package sfsoftware.util.map;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.newdawn.slick.opengl.Texture;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import sfsoftware.game.TrevorLegacy;
import sfsoftware.game.actor.*;
import sfsoftware.game.actor.enemy.*;
import sfsoftware.game.actor.enemy.bubblefish.*;
import sfsoftware.game.actor.item.*;
import sfsoftware.util.Script;
import sfsoftware.util.Tile;
import sfsoftware.util.graphics.Graphics;

public class MapReader {
	public static Properties properties = new Properties();		
	private static final String DEFAULT_TILE_TEXTURE;
	private static String mapFile;
	private static Document document = null;
	
	static
	{
		try 
		{
			 //load a properties file
			properties.load(new FileInputStream("res/game.properties"));
			
			// Property containing the default map file name			
			mapFile = properties.getProperty("default.mapfile");	
			setMapFile(mapFile);
		} 
		catch (SAXException s)
		{
			 s.printStackTrace();
		}
		catch (IOException e)
		{		
			e.printStackTrace();
		}
		
		DEFAULT_TILE_TEXTURE = properties.getProperty("default.tileset", "tiles1");		
	}
	
	public static String getMapAuthor()
	{
		return document.getDocumentElement().getAttribute("author");
	}

	public static int getMapHeight()
	{
		return Integer.valueOf(document.getDocumentElement().getAttribute("height"));
	}
	
	public static String getMapName()
	{
		return document.getDocumentElement().getAttribute("name");
	}
	
	public static int getMapWidth()
	{
		return Integer.valueOf(document.getDocumentElement().getAttribute("width"));
	}
	
	public static NodeList getNodeListFromXPath(String expression) throws XPathExpressionException
	{
		XPath xPath =  XPathFactory.newInstance().newXPath();
		NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
		
		return nodeList;
	}
	
	public static String getTagAttributeValue(Node node, String attributeName)
	{
		String nodeValue = null;
		if(node.getAttributes().getNamedItem(attributeName) != null)
		{
			nodeValue =node.getAttributes().getNamedItem(attributeName).getNodeValue();
		}
		
		return nodeValue;
	}
	
	private static int getXFromId(String textureName, int tileId)
	{
		Texture temp = Graphics.getTexture(textureName);
		int width = temp.getImageWidth();
		int height = temp.getImageHeight();
		int currentId = 1;
		
		for(int j = 0; j < (height - 40); j+= 40)
		{
			for(int i = 0; i < (width - 40); i+=40)
			{
				if(currentId == tileId)
				{
					return i;
				}
				currentId++;
			}
		}
		return 0;
	}		
	
	private static int getYFromId(String textureName, int tileId)
	{
		Texture temp = Graphics.getTexture(textureName);
		int width = temp.getImageWidth();
		int height = temp.getImageHeight();
		int currentId = 1;
		
		for(int j = 0; j < (height - 40); j+= 40)
		{
			for(int i = 0; i < (width - 40); i+=40)
			{
				if(currentId == tileId)
				{
					return j;
				}
				currentId++;
			}
		}
		return 0;
	}
	
	public static void loadActors(int roomX, int roomY)
	{
		// Load scripted actors
		String expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/scriptedActor";
		XPath xPath =  XPathFactory.newInstance().newXPath(); 
		try {		
			NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
			for (int i = 0; i < nodeList.getLength(); i++) 
			{			    
				String script = getTagAttributeValue(nodeList.item(i), "script"); 
			    if(!(script.startsWith("res/scripts/")))
			    {
			    	script = "res/scripts/" + script;
			    }
				String texture = getTagAttributeValue(nodeList.item(i), "texture");
			    int x = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "x"));
			    int y = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "y"));
			    int srcX = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "srcx"));
			    int srcY = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "srcy"));			    
			    int width = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "width"));
			    int height = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "height"));	
			    //System.out.println("new sfsoftware.game.actor.ScriptedActor(" + x + ", " + y + ", " + texture + ", " + width + ", " + height + ", " + srcX + ", " + srcY + ", " + script + ");");			    
			    
			    if(getTagAttributeValue(nodeList.item(i), "onlyIf") != null)
			    {
			    	String onlyIf = getTagAttributeValue(nodeList.item(i), "onlyIf");
			    	if(Script.evaluate(onlyIf))
			    	{
			    		// What type of Actor should be created?
			    		if(getTagAttributeValue(nodeList.item(i), "type") != null)
			    		{
			    			String actorType = getTagAttributeValue(nodeList.item(i), "type");
			    			if(actorType.equalsIgnoreCase("bumpable"))
			    			{
			    				new sfsoftware.game.actor.ScriptedActor(x, y, texture, width, height, srcX, srcY, script);
			    			}
			    			else if(actorType.equalsIgnoreCase("proximity"))			    				
			    			{
			    				int triggerDistance = 0;
			    				// The map creator should have specified a distance in pixels if the scripted actor is a proximity triggered one.
			    				if(getTagAttributeValue(nodeList.item(i), "triggerDistance") != null)
					    		{		
					    			triggerDistance = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "triggerDistance"));
					    		}
					    		else
					    		{
					    			System.out.println("Warning: No triggerDistance specified for actor. Using 0 pixels.");
					    		}			    		
					    		
			    				new sfsoftware.game.actor.ProximityTriggeredScriptedActor(x, y, texture, width, height, srcX, srcY, script, triggerDistance);
			    			}
			    			
			    		}
			    		else
			    		{
			    			// Default type for ScriptedActors are bumpable ones.
			    			new sfsoftware.game.actor.ScriptedActor(x, y, texture, width, height, srcX, srcY, script);	
			    		}
			    	}
			    }
			    else
			    {
			    	// What type of Actor should be created?
		    		if(getTagAttributeValue(nodeList.item(i), "type") != null)
		    		{
		    			String actorType = getTagAttributeValue(nodeList.item(i), "type");
		    			if(actorType.equalsIgnoreCase("bumpable"))
		    			{
		    				new sfsoftware.game.actor.ScriptedActor(x, y, texture, width, height, srcX, srcY, script);
		    			}
		    			else if(actorType.equalsIgnoreCase("proximity"))			    				
		    			{
		    				int triggerDistance = 0;
		    				// The map creator should have specified a distance in pixels if the scripted actor is a proximity triggered one.
		    				if(getTagAttributeValue(nodeList.item(i), "triggerDistance") != null)
				    		{		
				    			triggerDistance = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "triggerDistance"));
				    		}
				    		else
				    		{
				    			System.out.println("Warning: No triggerDistance specified for actor. Using 0 pixels.");
				    		}			    		
				    		
		    				new sfsoftware.game.actor.ProximityTriggeredScriptedActor(x, y, texture, width, height, srcX, srcY, script, triggerDistance);
		    			}
		    			
		    		}
		    		else
		    		{
		    			// Default type for ScriptedActors are bumpable ones.
		    			new sfsoftware.game.actor.ScriptedActor(x, y, texture, width, height, srcX, srcY, script);	
		    		}			    	
			    }
			}
		}
		catch (XPathExpressionException e) 
		{
			e.printStackTrace();
		}		
		
		// Load any signs... :-|
		expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/sign";
		xPath =  XPathFactory.newInstance().newXPath(); 
		try {		
			NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
			for (int i = 0; i < nodeList.getLength(); i++) 
			{			    
				String text = getTagAttributeValue(nodeList.item(i), "text");
			    int x = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "x"));
			    int y = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "y"));
			    
			    new Sign(x, y, "objects", 200, 0, 40, 40, text);
			}
		}
		catch (XPathExpressionException e) 
		{
			e.printStackTrace();
		}						

		// Load enemies...
		expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/enemy";
		xPath =  XPathFactory.newInstance().newXPath(); 
		try {		
			NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
			for (int i = 0; i < nodeList.getLength(); i++) 
			{			    
				String enemyType = getTagAttributeValue(nodeList.item(i), "type");
			    int x = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "x"));
			    int y = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "y"));			   			   
			    
				if(enemyType.equalsIgnoreCase("simplebubblefish"))
				{
					new SimpleBubbleFish(x, y, "bubblefish", 40, 40, TrevorLegacy.tileMap);
				}
				else if(enemyType.equalsIgnoreCase("pingpongbubblefishx"))
				{
					new PingPongBubbleFish(x, y, "bubblefish", 40, 40, TrevorLegacy.tileMap, PingPongBubbleFish.RIGHT_DIRECTION);
				}
				else if(enemyType.equalsIgnoreCase("pingpongbubblefishy"))
				{
					new PingPongBubbleFish(x, y, "bubblefish", 40, 40, TrevorLegacy.tileMap, PingPongBubbleFish.DOWN_DIRECTION);
				}
				else if(enemyType.equalsIgnoreCase("furyfish"))
				{
					new FuryFish(x, y, "bubblefish", 40, 40, TrevorLegacy.tileMap);
				}
				else if(enemyType.equalsIgnoreCase("simpleshooter"))
				{
					new SimpleShooter(x + 10, y + 2, "shooter", 20, 36, TrevorLegacy.tileMap);
				}
				else if(enemyType.equalsIgnoreCase("smartshooter"))
				{
					new SmartShooter(x + 10, y + 2, "shooter", 20, 36, TrevorLegacy.tileMap);
				}	
				else if(enemyType.equalsIgnoreCase("scubashooter"))
				{
					new ScubaShooter(x + 10, y + 2, "shooter", 20, 36, TrevorLegacy.tileMap);
				}
			}
		}
		catch (XPathExpressionException e) 
		{
			e.printStackTrace();
		}	
	
	}
	
	/**
	 * 	Loads Actors from a .properties file style map file
	 *
	 * 	@deprecated 	Use {@link loadActors(int, int)} instead.  
	 */
	@Deprecated
	public static Vector<sfsoftware.util.Actor> loadActors(Tile[][] tileMap, int x, int y, int sizeX, int sizeY)
	{
		Vector<sfsoftware.util.Actor> v = new Vector<sfsoftware.util.Actor>();
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(mapFile));
			for(int i = 0; i < sizeX; i++)
			{
				for(int j = 0; j < sizeY; j++)
				{	
					String s = prop.getProperty("room." + x + "." + y + "." + i + "." + j + ".actor","");
					if(s.equals("SimpleBubbleFish"))
					{
						v.add(new SimpleBubbleFish((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "bubblefish", 40, 40, tileMap));
					}
					if(s.equals("FuryFish"))
					{
						v.add(new FuryFish((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "bubblefish", 40, 40, tileMap));
					}
					else if(s.equals("Lava"))
					{
						v.add(new Lava((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY()));
						tileMap[i][j].setTexture("objects");
						tileMap[i][j].setSrcY(120);
					}
					else if(s.equals("Shooter"))
					{
						v.add(new SimpleShooter((int) tileMap[i][j].getX() + 10, (int) tileMap[i][j].getY() + 2, "shooter", 20, 36, tileMap));
					}
					else if(s.equals("SmartShooter"))
					{
						v.add(new SmartShooter((int) tileMap[i][j].getX() + 10, (int) tileMap[i][j].getY() + 2, "shooter", 20, 36, tileMap));
					}
					else if(s.equals("PingPongBubbleFishX"))
					{
						v.add(new PingPongBubbleFish((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "bubblefish", 40, 40, tileMap, PingPongBubbleFish.RIGHT_DIRECTION));
					}
					else if(s.equals("PingPongBubbleFishY"))
					{
						v.add(new PingPongBubbleFish((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "bubblefish", 40, 40, tileMap, PingPongBubbleFish.DOWN_DIRECTION));
					}	
					else if(s.equals("Ruple"))
					{						
						//TODO	Right now, a random ruple value is picked. Add specifics for each value.
						v.add(new Ruple((int) tileMap[i][j].getX() + 12, (int) tileMap[i][j].getY() + 12, "ruple", ((int)(Math.random() * 4)) * 16, 0, 16, 16));
					}
					else if(s.equals("ExplodableBlock"))
					{												
						v.add(new ExplodableBlock((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "objects", 0, 80, 40, 40));
					}
					else if(s.equals("Map"))
					{												
						v.add(new Item((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "items", 0, 40, 40, 40, "Map"));
					}
					else if(s.equals("Torch"))
					{												
						v.add(new Item((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "items", 0, 0, 40, 40, "Torch"));
					}
					else if(s.equals("Snowdust"))
					{												
						v.add(new Item((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "items", 40, 0, 40, 40, "Snow Dust"));
					}
					else if(s.equals("Bomb"))
					{												
						v.add(new Item((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "items", 80, 0, 40, 40, "Bomb"));
					}
					else if(s.equals("Sign"))
					{												
						v.add(new Sign((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "objects", 200, 0, 40, 40, "This is a sign!"));
					}
					else if(s.equals("Rock"))
					{												
						v.add(new PushableObject((int) tileMap[i][j].getX(), (int) tileMap[i][j].getY(), "objects", 0, 40, 40, 40));
					}	
				}
			}
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}

		return v;
	}

	public static Tile[][] loadBaseTiles(Tile[][] tileMap, int roomX, int roomY)
	{		
		// Reset all the tiles just in case the map file doesn't cover some of what we need.
		for(int xTile = 0; xTile < tileMap.length; xTile++)
		{
			for(int yTile = 0; yTile < tileMap[0].length; yTile++)
			{	
				tileMap[xTile][yTile].setTexture(DEFAULT_TILE_TEXTURE);				
				tileMap[xTile][yTile].setSrc(0, 0);
				tileMap[xTile][yTile].setWalkable(true);
			}
		}
		
		String expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/layer[@id='0']";
		XPath xPath =  XPathFactory.newInstance().newXPath(); 
		try
		{
			NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
			for (int i = 0; i < nodeList.getLength(); i++) 
			{
				String layerId = getTagAttributeValue(nodeList.item(i),"id");
				expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/walkability[@id='" + layerId + "']";
				Node walkability = ((NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET)).item(0);
				
				String layerTexture = getTagAttributeValue(nodeList.item(i),"texture");				
				String layerData = nodeList.item(i).getTextContent().trim();
				String walkabilityData = walkability.getTextContent().trim();
				
				String[] tileDatum = layerData.split(",");
				String[] walkabilityDatum = walkabilityData.split(",");
				
				int z = 0;				
				for(int xTile = 0; xTile < tileMap.length; xTile++)
				{
					for(int yTile = 0; yTile < tileMap[0].length; yTile++)
					{						
						tileMap[xTile][yTile].setTexture(layerTexture);												
						boolean w = true;						
						if(z < walkabilityDatum.length && Integer.valueOf(walkabilityDatum[z]) == 1)
						{
							w = false;							
						}						
						tileMap[xTile][yTile].setWalkable(w);
											
						if(z < tileDatum.length)
						{
							//System.out.println(tileDatum[z] + ": " + getXFromId(layerTexture, Integer.valueOf(tileDatum[z])) + ", " + getYFromId(layerTexture, Integer.valueOf(tileDatum[z])));							
							tileMap[xTile][yTile].setSrc(getXFromId(layerTexture, Integer.valueOf(tileDatum[z])), getYFromId(layerTexture, Integer.valueOf(tileDatum[z])));
						}
						z++;
					}					
				}
			}
		}
		catch (XPathExpressionException e) 
		{
			e.printStackTrace();
		}	
		return tileMap;
	}	
	
	public static void loadItems(int roomX, int roomY)
	{
		// Load items
		//System.out.println("Loading items...");
		String expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/item";
		XPath xPath =  XPathFactory.newInstance().newXPath(); 
		try {		
			NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
			//System.out.println(nodeList.getLength() + " items found.");
			for (int i = 0; i < nodeList.getLength(); i++) 
			{			    
				String itemType = getTagAttributeValue(nodeList.item(i), "type").trim(); 
				//System.out.println("item type: " + itemType);
				int x = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "x"));
			    int y = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "y"));
			    
			    if(itemType.equalsIgnoreCase("ruple"))
			    {
			    	int value = Integer.valueOf(getTagAttributeValue(nodeList.item(i), "value"));
			    	value--;
			    	if(value < 0)
			    	{
			    		value = 0;			    		
			    	}
			    	else if(value > 3)
			    	{
			    		value = 3;
			    	}
			    	//System.out.println("Creating ruple with a value of " + (value + 1) + " at " + x + ", " + y + "!");
			    	new Ruple((int) x + 12, (int) y + 12, "ruple", value * 16, 0, 16, 16);
			    }
			    else
			    {			    	
					String itemName = itemType.replaceAll("_", " ");
					String itemNameWithUnderscores = itemType.replaceAll(" ", "_"); 
					
					// Add an item to inventory
					String texture = Item.properties.getProperty(itemNameWithUnderscores.toLowerCase() + ".texture", "items");
					String source = Item.properties.getProperty(itemNameWithUnderscores.toLowerCase() + ".source", "0,0");
					String size = Item.properties.getProperty(itemNameWithUnderscores.toLowerCase() + ".size", "40,40");
										
					int srcX = Integer.valueOf(source.substring(0, source.lastIndexOf(",")).trim());
					int srcY = Integer.valueOf(source.substring(source.lastIndexOf(",") + 1, source.length()).trim());
					
					int width = Integer.valueOf(size.substring(0, size.lastIndexOf(",")).trim());
					int height = Integer.valueOf(size.substring(size.lastIndexOf(",") + 1, size.length()).trim());
					
					new Item(x, y, texture, srcX, srcY, width, height, itemName);			    	
			    }
				//String texture = getTagAttributeValue(nodeList.item(i), "texture");			     
			}
		}
		catch (XPathExpressionException e) 
		{
			e.printStackTrace();
		}		
	}
	
	public static Tile[][][] loadLayers(Tile[][][] layers, int roomX, int roomY)
	{
		int mapWidth = layers[0].length;
		int mapHeight = layers[0][0].length;

		for(int n = 0; n < layers.length; n++)
		{
			for(int xTile = 0; xTile < mapWidth; xTile++)
			{
				for(int yTile = 0; yTile < mapHeight; yTile++)
				{
					layers[n][xTile][yTile].setTexture("");
					layers[n][xTile][yTile].setSrc(0, 0);
				}
			}
			
			// Since the layer with id 0 will always be handled by the method loadBaseTiles, increment n by 1.
			String expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/layer[@id='" + (n + 1) + "']";
			XPath xPath =  XPathFactory.newInstance().newXPath(); 
			try
			{
				NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);			
				Node node = nodeList.item(0);
				if(node != null)
				{
					String layerTexture = getTagAttributeValue(node, "texture");
					String layerData = node.getTextContent().trim();
					String[] tileDatum = layerData.split(",");
					int z = 0;		
					for(int xTile = 0; xTile < mapWidth; xTile++)
					{
						for(int yTile = 0; yTile < mapHeight; yTile++)
						{
							
							if(z < tileDatum.length)
							{	
								if(Integer.valueOf(tileDatum[z]) > 0)
								{
									layers[n][xTile][yTile].setTexture(layerTexture);													
									layers[n][xTile][yTile].setSrc(getXFromId(layerTexture, Integer.valueOf(tileDatum[z])), getYFromId(layerTexture, Integer.valueOf(tileDatum[z])));
								}
							}						
							z++;
						}
					}
				}
			}
			catch (XPathExpressionException e) 
			{
				e.printStackTrace();
			}	
		}
		
		return layers;
	}	
	
	/**
	 * 	Loads layers from a .properties file style map file
	 *
	 * 	@deprecated 	Use {@link loadLayers(Tile[][][], int, int)} instead.  
	 */	
	@Deprecated
	public static Tile[][][] loadLayers(Tile[][][] layers, int x, int y, int sizeX, int sizeY)
	{
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(mapFile));
			for(int i = 0; i < sizeX; i++)
			{
				for(int j = 0; j < sizeY; j++)
				{	
					for(int n = 0; n < layers.length; n++)
					{
						String s = prop.getProperty("layer." + x + "." + y + "." + i + "." + j + "." + n +  ".texture", "");										
						if(s.indexOf(",") == -1)
						{
							layers[n][i][j].setTexture(s);
							layers[n][i][j].setSrcX(0);
							layers[n][i][j].setSrcY(0);
						}
						else
						{
							String[] splitString = s.split(",");
							layers[n][i][j].setTexture(splitString[0].trim());
							layers[n][i][j].setSrcX(Integer.valueOf(splitString[1].trim()));
							layers[n][i][j].setSrcY(Integer.valueOf(splitString[2].trim()));
						}
					}
				}
			}
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
		return layers;
	}
	
	/**
	 * 	Loads a room from a .properties file style map file
	 *
	 * 	@deprecated 	Use {@link loadBaseTiles(Tile[][], int, int)} instead.  
	 */	
	@Deprecated
	public static Tile[][] loadRoom(Tile[][] tileMap, int x, int y, int sizeX, int sizeY)
	{

		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(mapFile));
			for(int i = 0; i < sizeX; i++)
			{
				for(int j = 0; j < sizeY; j++)
				{	
					String s = prop.getProperty("room." + x + "." + y + "." + i + "." + j + ".walkable", "true");
					boolean b = Boolean.parseBoolean(s);					
					tileMap[i][j].setTexture(DEFAULT_TILE_TEXTURE);
					tileMap[i][j].setSrcX(0);
					tileMap[i][j].setSrcY(0);
					tileMap[i][j].setWalkable(b);
				}
			}
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}

		return tileMap;
	}
	
	public static void setMapFile(String newFileName) throws FileNotFoundException, SAXException, IOException
	{
		if(!(newFileName.startsWith("res/")))
		{
			newFileName = "res/" + newFileName;
		}
		
		mapFile = newFileName;		
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		
		try 
		{
			DocumentBuilder builder = builderFactory.newDocumentBuilder();
			document = builder.parse(new FileInputStream(mapFile));
		} 
		catch (ParserConfigurationException e) 
		{
		    e.printStackTrace();  
		}										
	}
}
